//
//  XDSettingViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-30.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDSettingViewController.h"
#import "XDLoginViewController.h"
#import "DBFrameService.h"
#import "XDServerIPSetViewController.h"
#import "CheckUpdateTask.h"
#import "UIViewController+MJPopupViewController.h"
#import "XDUpdatePanelViewController.h"
#import "StringUtil.h"
#import "CommonUtil.h"
#import "AttachManagerService.h"

@interface XDSettingViewController ()
{
    NSString *checkUpdateTaskGuid;
}
@end

@implementation XDSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"设置";
    [UITool setGlossyButtonWithRed:255.0 green:4.0f blue:61.0f :[self.view viewWithTag:1001]];
    
    if (![[DBFrameService getConfigValue:EP_ConfigKey_isLogin]isEqualToString:@"1"]) {
        [[self.view viewWithTag:1001] setHidden:YES];
    }
}
- (IBAction)quitLogin:(id)sender {
    [DBFrameService setConfigValue:EP_ConfigKey_password value:@""];
    XDLoginViewController *vc = [[XDLoginViewController alloc]init];
    [self.navigationController pushViewController:vc animated:NO];
    
    if ([[DBFrameService getConfigValue:EP_ConfigKey_isCleanAttach]isEqualToString:@"1"]) {
        [AttachManagerService deleteAllFile];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_btQuit release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setBtQuit:nil];
    [super viewDidUnload];
}

#pragma tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 2;
            break;
        case 2:
            return 1;
            break;

    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[[UITableViewCell alloc]init]autorelease];
    switch ([indexPath indexAtPosition:0]) {
        case 0:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    cell.textLabel.text = @"服务器设置";
                    cell.accessoryType = UITableViewCellSelectionStyleBlue;
                    break;
            }
            break;
        case 1:
            switch ([indexPath indexAtPosition:1]){
                    case 0:
                    cell.textLabel.text = @"自动登陆";
                    UISwitch *autoLoginSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(220, 8, 79, 27)];
                    [autoLoginSwitch addTarget:self action:@selector(autoLoginSwitchAction:) forControlEvents:UIControlEventValueChanged]; 
                    [cell.contentView addSubview:autoLoginSwitch];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    
                    if ([[DBFrameService getConfigValue:EP_ConfigKey_isAutoLogin]isEqualToString:@"1"]) {
                        [autoLoginSwitch setOn:YES];
                    }
                    else
                    {
                        [autoLoginSwitch setOn:NO];
                    }
                    
                    break;
                    case 1:
                    cell.textLabel.text = @"退出清空附件";
                    UISwitch *ssSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(220, 8, 79, 27)];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    [cell.contentView addSubview:ssSwitch];
                    [ssSwitch addTarget:self action:@selector(ssSwitchAction:) forControlEvents:UIControlEventValueChanged];
                    if ([[DBFrameService getConfigValue:EP_ConfigKey_isCleanAttach]isEqualToString:@"1"]) {
                        [ssSwitch setOn:YES];
                    }
                    else
                    {
                        [ssSwitch setOn:NO];
                    }
                    break;
            }
            break;
        case 2:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    cell.textLabel.text = @"软件更新";
                    cell.accessoryType = UITableViewCellSelectionStyleBlue;
                    break;
            }
            break;
    }
    return cell;
}

-(void)ssSwitchAction:(UISwitch *)sw
{
    if (sw.isOn) {
        [DBFrameService setConfigValue:EP_ConfigKey_isCleanAttach value:@"1"];
    }
    else
    {
        [DBFrameService setConfigValue:EP_ConfigKey_isCleanAttach value:@"0"];
    }
}

-(void)autoLoginSwitchAction:(UISwitch *)sw
{
    if (sw.isOn) {
        [DBFrameService setConfigValue:EP_ConfigKey_isAutoLogin value:@"1"];
    }
    else
    {
        [DBFrameService setConfigValue:EP_ConfigKey_isAutoLogin value:@"0"];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath indexAtPosition:0]) {
        case 0:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    [self gotoServerIpSet];
                    break;
            }
            break;
            case 2:
            switch ([indexPath indexAtPosition:1]){
                case 0:
                    NSLog(@"检测软件更新");
                    checkUpdateTaskGuid = [[[CheckUpdateTask alloc]initWithContext:self andParams:self.getPassDictionary]startTask];
                    break;
            }
            break;
    }
    
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    if ([threadGuid isEqualToString:checkUpdateTaskGuid]) {
        NSString *bs = (NSString *)obj;
        NSString *result = [StringUtil getXMLFormatedAttribute:bs :@"result"];
        
        
        
        if ([result isEqualToString:@"YES"]) {
            NSString *info = [StringUtil getXMLFormatedAttribute:bs :@"info"];
            NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
            [params setObject:info forKey:@"updateinfo"];
            XDUpdatePanelViewController *vc = [[XDUpdatePanelViewController alloc]initWithParams:params];
            vc.delegate = self;
            [self presentPopupViewController:vc animationType:MJPopupViewAnimationSlideBottomBottom];
        }
        else
        {
            [UITool toast:@"当前已经是最新版本！"];
        }
    }
}

#pragma control

-(void)gotoServerIpSet
{
    XDServerIPSetViewController *servervc = [[XDServerIPSetViewController alloc]init];
    [self.navigationController pushViewController:servervc animated:YES];

}

-(void)okAction
{
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideBottomTop];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",[CommonUtil getDownloadUrl:@"updateclient/dis.plist"]]]];
}

-(void)cancelPopWindow
{
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideBottomBottom];
}

@end
